/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hongkang.utils;

import com.hongkang.ecg.Constant;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;


public class PreferenceUtils {
	private static final String TAG = "PreferenceUtils";
	private static final boolean DEBUG = Constant.DEBUG;
	
	private static final String PREFERENCES_NAME = Constant.PACKAGE_NAME + "_preferences";
	
	/*
	 * 
	 */
//	public static boolean getBooleanPref(String name, boolean def) {
//		if (DEBUG) Log.d(TAG, "getBooleanPref: " + name);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		return prefs.getBoolean(name, def);
//	}
//	
//	public static void setBooleanPref(String name, boolean value) {
//		if (DEBUG) Log.d(TAG, "setBooleanPref: " + name + ", " + value);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		Editor ed = prefs.edit();
//		ed.putBoolean(name, value);
//		ed.commit();
//	}
	
	public static boolean getBooleanPref(Context context, String name, boolean def) {
		if (DEBUG) Log.d(TAG, "getBooleanPref: " + name);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return prefs.getBoolean(name, def);
	}
	
	public static void setBooleanPref(Context context, String name, boolean value) {
		if (DEBUG) Log.d(TAG, "setBooleanPref: " + name + ", " + value);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor ed = prefs.edit();
		ed.putBoolean(name, value);
		ed.commit();
	}
	
	/*
	 * 
	 */
//	public static int getIntPref(String name, int def) {
//		if (DEBUG) Log.d(TAG, "getIntPref: " + name);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		return prefs.getInt(name, def);
//	}
//	
//	public static void setIntPref(String name, int value) {
//		if (DEBUG) Log.d(TAG, "setIntPref: " + name + ", " + value);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		Editor ed = prefs.edit();
//		ed.putInt(name, value);
//		ed.commit();
//	}
	
	public static int getIntPref(Context context, String name, int def) {
		if (DEBUG) Log.d(TAG, "getIntPref: " + name);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return prefs.getInt(name, def);
	}
	
	public static void setIntPref(Context context, String name, int value) {
		if (DEBUG) Log.d(TAG, "setIntPref: " + name + ", " + value);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor ed = prefs.edit();
		ed.putInt(name, value);
		ed.commit();
	}
	
	/*
	 * 
	 */
//	public static long getLongPref(String name, long def) {
//		if (DEBUG) Log.d(TAG, "getLongPref: " + name);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		return prefs.getLong(name, def);
//	}
//	
//	public static void setLongPref(String name, long value) {
//		if (DEBUG) Log.d(TAG, "setLongPref: " + name + ", " + value);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		Editor ed = prefs.edit();
//		ed.putLong(name, value);
//		ed.commit();
//	}
	
	public static long getLongPref(Context context, String name, long def) {
		if (DEBUG) Log.d(TAG, "getLongPref: " + name);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return prefs.getLong(name, def);
	}
	
	public static void setLongPref(Context context, String name, long value) {
		if (DEBUG) Log.d(TAG, "setLongPref: " + name + ", " + value);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor ed = prefs.edit();
		ed.putLong(name, value);
		ed.commit();
	}
	
	/*
	 * 
	 */
//	public static float getFloatPref(String name, float def) {
//		if (DEBUG) Log.d(TAG, "getFloatPref: " + name);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		return prefs.getFloat(name, def);
//	}
//	
//	public static void setFloatPref(String name, float value) {
//		if (DEBUG) Log.d(TAG, "setFloatPref: " + name + ", " + value);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		Editor ed = prefs.edit();
//		ed.putFloat(name, value);
//		ed.commit();
//	}

	public static float getFloatPref(Context context, String name, float def) {
		if (DEBUG) Log.d(TAG, "getFloatPref: " + name);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return prefs.getFloat(name, def);
	}
	
	public static void setFloatPref(Context context, String name, float value) {
		if (DEBUG) Log.d(TAG, "setFloatPref: " + name + ", " + value);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor ed = prefs.edit();
		ed.putFloat(name, value);
		ed.commit();
	}

	/*
	 * 
	 */
//	public static String getStringPref(String name, String def) {
//		if (DEBUG) Log.d(TAG, "getStringPref: " + name);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		return prefs.getString(name, def);
//	}
//	
//	public static void setStringPref(String name, String value) {
//		if (DEBUG) Log.d(TAG, "setStringPref: " + name + ", " + value);
//		SharedPreferences prefs = Kankan.getContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//		Editor ed = prefs.edit();
//		ed.putString(name, value);
//		ed.commit();
//	}

	public static String getStringPref(Context context, String name, String def) {
		if (DEBUG) Log.d(TAG, "getStringPref: " + name);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return prefs.getString(name, def);
	}
	
	public static void setStringPref(Context context, String name, String value) {
		if (DEBUG) Log.d(TAG, "setStringPref: " + name + ", " + value);
		SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor ed = prefs.edit();
		ed.putString(name, value);
		ed.commit();
	}
}

